//
//  SecondViewController.m
//  League
//
//  Created by 博宇 李 on 2018/1/18.
//  Copyright © 2018年 ciyuanjuzhen. All rights reserved.
//

#import "SecondViewController_.h"
#import "XFSegmentView.h"
#import "RYMessageViewController.h"

#import "OthersProfileViewController.h"
#import "MessagesCell.h"
#import "RCDChatListCell.h"
#import "RCDListCell.h"
#define kCellHeight 150
#define kSegmentH 40
#define kImageViewHW 90
#define kCellSpace 10

#define kSelectIndex1Hasfix @"CuicanRematch"
#define kSelectIndex2Hasfix @"CuicanFriend"

#define kEducaLabFontColor [UIColor colorWithRed:207/255.0 green:207/255.0 blue:207/255.0 alpha:1]
#define kTimeFontColor [UIColor colorWithRed:180/255.0 green:180/255.0 blue:180/255.0 alpha:1]
@interface SecondViewController_ ()<XFSegmentViewDelegate,RCIMUserInfoDataSource>
{
    NSInteger selectIndex;
    NSBundle *bundle;
    
    NSString *testStr ;
    UIButton *rematch;
    UILabel *messagesLab ;
    BOOL status;
    NSString *statueStr ;
    
    NSMutableArray *saveUserInfoList;
    
}
@property(strong , nonatomic)UITableView *tableV;
@property(strong , nonatomic)XFSegmentView *segView;
@property(strong , nonatomic)NSMutableArray *titleArr;
@property(strong , nonatomic)RCConversationListViewController *rcVc;
@end

@implementation SecondViewController_
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].applicationIconBadgeNumber =  [[RCIMClient sharedRCIMClient] getTotalUnreadCount];
    
    UITabBarItem *item = [self.tabBarController.tabBar.items objectAtIndex:1];
        if([[RCIMClient sharedRCIMClient] getTotalUnreadCount]> 0){
            
            item.badgeValue = [NSString stringWithFormat:@"%@",@([[RCIMClient sharedRCIMClient] getTotalUnreadCount])];
            if (@available(iOS 10.0, *)) {
                item.badgeColor = kAppColor;
            } else {
                // Fallback on earlier versions
               
            }
        }
         else
            {
                item.badgeValue = [NSString stringWithFormat:@"%@",@""];
                if (@available(iOS 10.0, *)) {
                    item.badgeColor = [UIColor clearColor];
                } else {
                    // Fallback on earlier versions
                }
            }
    [self refreshConversationTableViewIfNeeded];
    [self.conversationListTableView reloadData];
        
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
}
-(id)init
{
    self = [super init];
    if (self) {
        [super viewDidLoad];
         UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
        self.navigationItem.backBarButtonItem = item;
        NSString *lan = [InternationalControl userLanguage];
//        BaseViewController *vc = [[BaseViewController alloc] init];
//        vc.view.frame = CGRectMake(0, 100, self.view.width, self.view.height);
//        [self.view addSubview: vc.view];
        testStr = @"";
        if (@available(iOS 11.0, *)) {
            self.conversationListTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        
        NSLog(@"----%@",self.view.subviews);
      
//        if([lan isEqualToString:@"en"]){//判断当前的语言，进行改变
//        bundle = [InternationalControl bundle];
        saveUserInfoList = [NSMutableArray array];
        

        [self SegmentView];
//        self.cellBackgroundColor = [UIColor redColor];
        [self setConversationPortraitSize :CGSizeMake(kImageViewHW, kImageViewHW)];
        self.conversationListTableView.delegate = self;
        self.conversationListTableView.dataSource = self;
        self.conversationListTableView.separatorColor = [UIColor blackColor];
        self.conversationListTableView.tableFooterView = [UIView new];

        [self setDisplay];
        
    }
    return self;
}
-(void)setDisplay
{
    if (selectIndex == 3||selectIndex == 4) {
        [self setDisplayConversationTypes:@[
                                            @(ConversationType_GROUP)]];
    }
    else{
        
        
        [self setDisplayConversationTypes:@[@(ConversationType_PRIVATE),
                                            @(ConversationType_DISCUSSION),
                                            @(ConversationType_CHATROOM),
                                            @(ConversationType_APPSERVICE),
                                            @(ConversationType_SYSTEM)]];
    }
    
    if (selectIndex == 1||selectIndex == 2) {
        self.tableV.hidden = NO;
    }
    else
    {
        self.tableV.hidden = YES;
    }
    
    
}
/*
-(void)willDisplayConversationTableCell:(RCConversationBaseCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    RCConversationModel *model = self.conversationListDataSource[indexPath.row];//数据源
//    if (selectIndex == 2) {
//
//        [self setDisplayConversationTypes:@[@(ConversationType_GROUP)]];
////        [self  init];
//    }
//    else
//    {
//        [self setDisplayConversationTypes:@[@(ConversationType_PRIVATE),
//                                            @(ConversationType_DISCUSSION),
//                                            @(ConversationType_CHATROOM),
//                                            @(ConversationType_GROUP),
//                                            @(ConversationType_APPSERVICE),
//                                            @(ConversationType_SYSTEM)]];
//    }

    
     RCConversationCell *ConversationCell = (RCConversationCell *)cell ;
    if (selectIndex == 1) {
//    [rematch removeFromSuperview];
            rematch = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 240, 30)];
            rematch.center = CGPointMake(kCellSpace*2+kImageViewHW+rematch.width/2, cell.height- rematch.height/2);
            rematch.backgroundColor = kAppColor;
            rematch.layer.masksToBounds = YES;
            rematch.layer.cornerRadius = 8;
            rematch.tag = indexPath.row;
            [rematch setTitle:[bundle localizedStringForKey:@"rematch" value:nil table:@"hello"] forState:UIControlStateNormal];
            rematch.titleLabel.font=[UIFont fontWithName:kFontName size:rematch.height*0.5];
            [ConversationCell addSubview:rematch];
        NSLog(@"rematch的Tag:%@",@(rematch.tag));
    }
    else{
//        re
       
        
    }
        
//        rematch . hidden = NO;
//        ConversationCell.messageContentLabel.hidden = YES;
        if(model.unreadMessageCount >0){//有未读消息  消息颜色显示
            ConversationCell.messageContentLabel.textColor = kAppColor;
        }
        else
        {
            ConversationCell.messageContentLabel.textColor = [UIColor blackColor];
        }
    
   
    
 
    
}
 */
-(void)testBtn:(UIButton *)btn
{
    NSLog(@"1123123123123123");
    
}
- (void)refreshConversationTableViewWithConversationModel:(RCConversationModel *)conversationModel {
    
    conversationModel.conversationType = RC_CONVERSATION_MODEL_TYPE_CUSTOMIZATION;
    
    [super refreshConversationTableViewWithConversationModel:conversationModel];
    
}
- (void)onSelectedTableRow:(RCConversationModelType)conversationModelType
         conversationModel:(RCConversationModel *)model
               atIndexPath:(NSIndexPath *)indexPath {
    RYMessageViewController *conversationVC = [[RYMessageViewController alloc]init];
    conversationModelType = RC_CONVERSATION_MODEL_TYPE_CUSTOMIZATION;
    conversationVC.conversationType = model.conversationType;
    conversationVC.targetId = model.targetId;
    conversationVC.title = @"想显示的会话标题";
    conversationVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:conversationVC animated:YES];
}
-(void)segmentView:(XFSegmentView *)segmentView didSelectIndex:(NSInteger)index
{
     NSLog(@"%@",self.conversationListDataSource);
    NSLog(@"Tag:%@",@(index));
    selectIndex = index;

    [saveUserInfoList removeAllObjects];

    testStr = @"我刷新了";
    [self setDisplay];
   
   [self refreshConversationTableViewIfNeeded];
   [self.conversationListTableView reloadData];
}

-(void)getUserInfoWithID:(NSString *)userId{
    //网络请求
    NSDictionary *dict = @{@"userName":@"gdgk",@"userPhoto":@"bddgdg",@"id":@"123"};
//    NSMutableDictionary *mutaDic = dict.mutableCopy;
//    [mutaDic addEntriesFromDictionary:@{@"tageID":@"1235"}];
    [saveUserInfoList addObject:dict];
}

-(NSMutableArray *)willReloadTableData:(NSMutableArray *)dataSource
{
    if (selectIndex == 0) {
        
        [dataSource enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            RCConversationModel *model = (RCConversationModel *)obj;
            NSLog(@"%@",model.targetId);
            [self getUserInfoWithID:model.targetId];
        }];
        
        if (saveUserInfoList.count > 0) {
            [self.conversationListTableView reloadData];
        }
        
    }
    
  
    
    
    
    for (int i=0; i<dataSource.count ; i++){
        
        RCConversationModel *model = dataSource[i];
            
            // 会话Cell数据模型的显示类型 转化为自定义类型
            
            model.conversationModelType = RC_CONVERSATION_MODEL_TYPE_CUSTOMIZATION;
        
        
    }
    
    return dataSource;

}

// 高度
-(CGFloat)rcConversationListTableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RCConversationModel *model = self.conversationListDataSource[indexPath.row];
    if (selectIndex == 0) {
        return [model.targetId hasPrefix:kSelectIndex1Hasfix]?0 : kCellHeight;
    }
    else if(selectIndex == 1)
    {
        return [model.targetId hasPrefix:kSelectIndex1Hasfix]?kCellHeight : 0;
    }
    else if (selectIndex == 2)
    {
        return [model.targetId hasPrefix:kSelectIndex2Hasfix]?kCellHeight : 0;
    }
    return kCellHeight;
}

- (void)didTapCellPortrait:(RCConversationModel *)model{
    NSLog(@"list点击头像的id:%@",model.targetId);
    OthersProfileViewController *otherProVc = [[OthersProfileViewController alloc] init];
    [self.navigationController pushViewController:otherProVc animated:YES];
}

-(void)SegmentView
{
    
   
    
    if (kIs_Iphone_X)
    {
        
    }
    self.segView=[[XFSegmentView alloc]initWithFrame:Frame(0, kNaviHeight, SCREEN_WIDTH, kSegmentH)];
    [self.view addSubview:self.segView];
    self.segView.delegate = self;

    NSString *lan = [InternationalControl userLanguage];
    if([lan isEqualToString:@"en"]){
        
        self.segView.titles =
        @[@"Match",
          @"Expired", @"Friends",@"Events",@"Groups"];
    }
    else
    {
        self.segView.titles =
        @[@"相配",
          @"过期", @"好友",@"聚一聚",@"聊一聊"];
    }
    if (kIs_Iphone_X) {
        self.segView.titleFont = [UIFont fontWithName:kFontName size:14];
        CGRect tableFrame = self.conversationListTableView .frame;
        tableFrame.origin.y = self.segView.height+88;
        tableFrame.size.height = tableFrame.size.height-88-83-self.segView.height;
        self.conversationListTableView.frame = tableFrame;
    }
    else
    {
        self.segView.titleFont = [UIFont fontWithName:kFontName size:14];
        CGRect tableFrame = self.conversationListTableView .frame;
        tableFrame.origin.y = self.segView.height+64;
        tableFrame.size.height = tableFrame.size.height-64-49-self.segView.height;
        self.conversationListTableView.frame = tableFrame;
    }
    
    
    self.conversationListTableView.delegate = self;
    self.conversationListTableView.dataSource = self;
    
    
    [self TableView];
}
-(void)TableView
{
    self.tableV = [[UITableView alloc] initWithFrame:self.conversationListTableView.frame];//指定位置大小
    self.tableV.backgroundColor = [UIColor redColor];
    [self.tableV setDelegate:self];//指定委托
    [self.tableV setDataSource:self];//指定数据委托
    self.tableV.estimatedRowHeight = 0;
    self.tableV.estimatedSectionHeaderHeight = 0;
    self.tableV.estimatedSectionFooterHeight = 0;
    self.tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
 //   [self.view addSubview:self.tableV];//加载tableview
    
    
    
}
- (void)didReceiveMessageNotification:(NSNotification *)notification
{
    
//    NSLog(@"%@",notification);
//    NSLog(@"%@",@([[RCIMClient sharedRCIMClient] getTotalUnreadCount]));
////    [[RCIMClient sharedRCIMClient] getTotalUnreadCount];
//    self.tabBarItem.badgeValue = [NSString stringWithFormat:@"%@",@([[RCIMClient sharedRCIMClient] getTotalUnreadCount])];
//    if (@available(iOS 10.0, *)) {
//        self.tabBarItem.badgeColor = kAppColor;
//    } else {
//        // Fallback on earlier versions
//    }
//
   [self refreshConversationTableViewIfNeeded];
//    [self.conversationListTableView reloadData];
     [UIApplication sharedApplication].applicationIconBadgeNumber =  [[RCIMClient sharedRCIMClient] getTotalUnreadCount];
    if([[RCIMClient sharedRCIMClient] getTotalUnreadCount]> 0){
        UITabBarItem *item = [self.tabBarController.tabBar.items objectAtIndex:1];
        item.badgeValue = [NSString stringWithFormat:@"%@",@([[RCIMClient sharedRCIMClient] getTotalUnreadCount])];
        if (@available(iOS 10.0, *)) {
            item.badgeColor = kAppColor;
        } else {
            // Fallback on earlier versions
        }
    }
    
//    UITabBarItem *item = [self.tabBarController.tabBar.items objectAtIndex:2];
//    item.badgeValue =  [NSString stringWithFormat:@"%@",@([[RCIMClient sharedRCIMClient] getTotalUnreadCount])];
//    [[RCIMClient sharedRCIMClient] getTotalUnreadCount];

}
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    UITableViewRowAction *action0 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"关注" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
//
//    }];
    
//    [[RCIMClient sharedRCIMClient] removeConversation:model.conversationType targetId:model.targetId];
//
//    [[RCIMClient sharedRCIMClient]clearMessages:model.conversationType targetId:model.targetId];
//    [self refreshConversationTableViewIfNeeded];
//    [self.conversationListTableView reloadData];
//
    [InternationalControl initUserLanguage];//初始化应用语言
    
    NSBundle *bundle = [InternationalControl bundle];
    
    

        RCConversationModel *model = self.conversationListDataSource[indexPath.row];
        
        [[RCIMClient sharedRCIMClient] getConversationNotificationStatus:model.conversationType targetId:model.targetId success:^(RCConversationNotificationStatus nStatus) {
            if (nStatus == NOTIFY) {
                statueStr = [bundle localizedStringForKey:@"NOblock" value:nil table:@"hello"];
            
            }
            else if (nStatus == DO_NOT_DISTURB)
            {
                statueStr = statueStr = [bundle localizedStringForKey:@"block" value:nil table:@"hello"];;
         
            }
            else
            {
                statueStr = @"不知道";
     
            }
            
        } error:^(RCErrorCode status) {
            NSLog(@"失败了");

        }];

    if (statueStr) {
        
    }
    else
    {
        sleep(1);
    }

   return  [self ListrowAction];
   
}
-(NSArray *)ListrowAction
{
    UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:statueStr handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        RCConversationModel *model = self.conversationListDataSource[indexPath.row];
        
        
        [[RCIMClient sharedRCIMClient] getConversationNotificationStatus:model.conversationType targetId:model.targetId success:^(RCConversationNotificationStatus nStatus) {
            NSLog(@"nStatus:%@",@(nStatus));//DO_NOT_DISTURB 免打扰  NOTIFY 新消息提醒
            if (nStatus == NOTIFY) {
                
                [[RCIMClient sharedRCIMClient] setConversationNotificationStatus:model.conversationType targetId:model.targetId isBlocked:YES success:^(RCConversationNotificationStatus nStatus) {
                    NSLog(@"成功了");
                    //                    [self refreshConversationTableViewIfNeeded];
                    //                    [self.conversationListTableView reloadData];
                } error:^(RCErrorCode status) {
                    NSLog(@"失败了");
                }];
                
            }
            else
            {
                [[RCIMClient sharedRCIMClient] setConversationNotificationStatus:model.conversationType targetId:model.targetId isBlocked:NO success:^(RCConversationNotificationStatus nStatus) {
                    NSLog(@"成功了");
                    //                    [self refreshConversationTableViewIfNeeded];
                    //                    [self.conversationListTableView reloadData];
                } error:^(RCErrorCode status) {
                    NSLog(@"失败了");
                }];
                
            }
        } error:^(RCErrorCode status) {
            NSLog(@"nStatus:%@",@(status));
            
        }];
        
        
        
    }];
    
     UITableViewRowAction *action2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"过期" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
         RCConversationModel *model = self.conversationListDataSource[indexPath.row];
         
         
//         [[RCIMClient sharedRCIMClient] removeConversation:model.conversationType targetId:model.targetId];
//
//         [[RCIMClient sharedRCIMClient]clearMessages:model.conversationType targetId:model.targetId];
         }];
    if (selectIndex == 0) {
        return nil;
    }else if(selectIndex == 1||selectIndex == 2||selectIndex == 3||selectIndex == 4)
    {
         return @[action1,action2];
    }else
    {
        return nil;
    }
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(selectIndex == 0)
    {
        return NO;
    }else
    {
        return YES;
    }
    
}
//-(void)getUserInfoWithUserId:(NSString *)userId completion:(void (^)(RCUserInfo *))completion
//{
//
//}
-(void)rcConversationListTableView:(UITableView *)tableView

commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {


//        UITableViewRowAction *action0 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"关注" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
//
//        }];
//
//        UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
//
//        }];
//
//        UITableViewRowAction *action2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"其他" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
//
//        }];
//        return @[action0, action1, action2];
    RCConversationModel *model = self.conversationListDataSource[indexPath.row];
    [[RCIMClient sharedRCIMClient] removeConversation:model.conversationType targetId:model.targetId];

   [[RCIMClient sharedRCIMClient]clearMessages:model.conversationType targetId:model.targetId];

}
- (RCConversationBaseCell *)rcConversationListTableView:(UITableView *)tableView
                                  cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    
    RCConversationModel *model = self.conversationListDataSource[indexPath.row];
    static NSString *CellIdentifier = @"TableViewCell";
    // 通过indexPath创建cell实例 每一个cell都是单独的
    RCDChatListCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    // 判断为空进行初始化  --（当拉动页面显示超过主页面内容的时候就会重用之前的cell，而不会再次初始化）
    if (!cell) {
        cell = [[RCDChatListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.width = tableView.width;
    }
  
    
//    cell.isShow = NO;
//    if (isShwow) {
//        cell.isShow = YES;
//        cell.nameLabel.text = @"xingming";
//        cell.infoOen.text = @"sfsfsf";
//        cell.infoTwo.text = @"vvcvcvc";
//        cell.lastMessage.text = @"vdvd";
//    }else{
//        cell.avtar.hidden = YES;
//        cell.rematchBtn.hidden = YES;
//        cell.nameLabel.hidden = YES;
//        cell.infoOen.hidden = YES;
//        cell.infoTwo.hidden = YES;
//        cell.lastMessage.hidden = YES;
//    }
    
    
//    RCDChatListCell *cell = [[RCDChatListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
//    if (!cell) {
//        //        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
//
//            }
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    cell.width = tableView.width;
////    cell.height = kCellHeight;
//    RCConversationModel *model = self.conversationListDataSource[indexPath.row];
//    ;
//    if (![model.targetId hasPrefix:@"Cuican"]) {
//
//    }
//
//
//    RCUserInfo *userInfo = self.conversationListDataSource[indexPath.row];
    

    NSInteger space = 10;
    UIImageView *userImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kCellHeight*0.7,kCellHeight*0.7 )];
    userImage.center = CGPointMake(space+userImage.width/2, cell.center.y+userImage.height/2);
    //        userImage.backgroundColor = [UIColor redColor];
    userImage.layer.masksToBounds = YES;
    userImage.layer.cornerRadius = 8;
    [cell.contentView addSubview:userImage];
    [userImage sd_setImageWithURL:[NSURL URLWithString:[[RCIM sharedRCIM] getUserInfoCache:model.targetId].portraitUri]];
    userImage.userInteractionEnabled = YES;
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toOthersProfile)];
    //为图片添加手势
    [userImage addGestureRecognizer:singleTap];

    UILabel *nameLab = [[UILabel alloc] initWithFrame:CGRectMake(userImage.x+userImage.width+space, userImage.y, cell.width - userImage.width - 3*space, userImage.height/3.5)];
    //        nameLab.backgroundColor = [UIColor yellowColor];
            [cell addSubview:nameLab];
        nameLab.font=[UIFont fontWithName:kFontName size:nameLab.height*0.6];
        nameLab.text=[[RCIM sharedRCIM] getUserInfoCache:model.targetId].name;

            UILabel *infoLab = [[UILabel alloc] initWithFrame:CGRectMake(nameLab.x, nameLab.y+nameLab.height, nameLab.width, userImage.height/4)];
    //        infoLab.backgroundColor = [UIColor greenColor];
            [cell addSubview:infoLab];
        infoLab.font=[UIFont fontWithName:kFontName size:infoLab.height*0.5];
        infoLab.text = @"35.Marina/West Village.5 ' l ";

            UILabel *educaLab = [[UILabel alloc] initWithFrame:CGRectMake(infoLab.x, infoLab.y+infoLab.height, nameLab.width, userImage.height/4)];
    //        educaLab.backgroundColor = [UIColor redColor];
            [cell addSubview:educaLab];
        educaLab.font=[UIFont fontWithName:kFontName size:educaLab.height*0.5];
        educaLab.textColor = kEducaLabFontColor;
        educaLab.text = @"Goldman Sachs. Harvard Law School";

        UILabel *timeLab = [[UILabel alloc] initWithFrame:nameLab.frame];
        timeLab.font=[UIFont fontWithName:kFontName size:timeLab.height*0.4];
        timeLab.textAlignment = NSTextAlignmentRight;
        timeLab.text = @"21 day ago";
       timeLab.textColor = kTimeFontColor;
        [cell addSubview:timeLab];


         if (selectIndex == 1)
        {



            rematch = [[UIButton alloc] initWithFrame:CGRectMake(educaLab.x, educaLab.y+educaLab.height, nameLab.width, userImage.height/3.5)];
            rematch.backgroundColor = kAppColor;
            rematch.layer.masksToBounds = YES;
            rematch.layer.cornerRadius = 8;
            [cell addSubview:rematch];
            NSString *lan = [InternationalControl userLanguage];
            if([lan isEqualToString:@"en"]){
                [rematch setTitle:@"rematch" forState:UIControlStateNormal];
            }
            else
            {
                [rematch setTitle:@"叙叙旧" forState:UIControlStateNormal];
            }

            rematch.titleLabel.font=[UIFont fontWithName:kFontName size:rematch.height*0.5];
            }
        else
        {

            messagesLab = [[UILabel alloc] initWithFrame:CGRectMake(educaLab.x, educaLab.y+educaLab.height, nameLab.width,userImage.height/4)];
    //        messagesLab.backgroundColor = [UIColor brownColor];
            
            messagesLab.font=[UIFont fontWithName:kFontName size:messagesLab.height*0.5];


            NSLog(@"model.lastestMessage.conversationDigest:%@",model.lastestMessage);

            if ([model.lastestMessage isKindOfClass:[RCTextMessage class]]) {
               RCTextMessage *textMsg = (RCTextMessage *)model.lastestMessage;
                messagesLab.text = textMsg.content;

                NSLog(@"文字");
            } else if ([model.lastestMessage isKindOfClass:[RCImageMessage class]]) {
                messagesLab.text = @"[图片]";
                NSLog(@"图片");
            } else if ([model.lastestMessage isKindOfClass:[RCVoiceMessage class]]) {
                NSLog(@"语音");
                messagesLab.text = @"[语音]";

            } else if ([model.lastestMessage isKindOfClass:[RCLocationMessage class]]) {
                messagesLab.text = @"[位置]";
                NSLog(@"位置");

            } else if ([model.lastestMessage isKindOfClass:[RCDiscussionNotificationMessage class]]) {


            } else {
                NSLog(@"不支持消息");
                messagesLab.text = @"[其他消息]";
            }
[cell addSubview:messagesLab];


//            messagesLab.text = model.lastestMessage.conversationDigest;

//            if (indexPath.row == 0 ) {
//                messagesLab.textColor = kAppColor;
//            }
            if(model.unreadMessageCount >0){//有未读消息  消息颜色显示
                messagesLab.textColor = [UIColor colorWithRed:118/255.0 green:191/255.0 blue:241/255.0 alpha:1];
            }
            else
            {
                messagesLab.textColor = [UIColor blackColor];
            }




        }
    
    BOOL isShwow = false;
    if (selectIndex == 0) {
        isShwow = [model.targetId hasPrefix:kSelectIndex1Hasfix] ? NO : YES;
        if (isShwow) {
            
        }
        else
        {
            userImage.hidden = YES;
            nameLab.hidden = YES;
            infoLab.hidden = YES;
            educaLab.hidden = YES;
            rematch.hidden = YES;
            timeLab.hidden = YES;
            messagesLab.hidden = YES;
        }
        
    }else if(selectIndex == 1){
        isShwow = [model.targetId hasPrefix:kSelectIndex1Hasfix] ? YES : NO;
        if (isShwow) {
            
        }
        else
        {
            userImage.hidden = YES;
            nameLab.hidden = YES;
            infoLab.hidden = YES;
            educaLab.hidden = YES;
            rematch.hidden = YES;
            timeLab.hidden = YES;
            messagesLab.hidden = YES;
        }
        
    }
    else if (selectIndex == 2)
    {
         isShwow = [model.targetId hasPrefix:kSelectIndex2Hasfix] ? YES : NO;
        if (isShwow) {
            rematch.hidden = YES;
            messagesLab.hidden = YES;
        }
        else{
            userImage.hidden = YES;
            nameLab.hidden = YES;
            infoLab.hidden = YES;
            educaLab.hidden = YES;
            rematch.hidden = YES;
            timeLab.hidden = YES;
            messagesLab.hidden = YES;
        }
        
        
    }
    
    [self getUserInfoWithUserId:model.targetId completion:^(RCUserInfo *userInfo) {
        nameLab.text = userInfo.name;
        [userImage sd_setImageWithURL:[NSURL URLWithString:userInfo.portraitUri]];
    }];
    
    
    if (saveUserInfoList.count > 0) {
        [saveUserInfoList enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSDictionary *dict = (NSDictionary *)obj;
            if ([model.targetId isEqualToString:dict[@"id"]]) {
                //这里赋值给控件
            }
        }];
    }
    return cell;
}
-(void)toOthersProfile
{
        OthersProfileViewController *otherProVc = [[OthersProfileViewController alloc] init];
        [self.navigationController pushViewController:otherProVc animated:YES];
}

- (void)getUserInfoWithUserId:(NSString *)userId completion:(void (^)(RCUserInfo *))completion {
    
    
    NSLog(@"个人----%@",userId);
    if ([userId isEqualToString:@"ctest3"]) {
        RCUserInfo *userInfo = [[RCUserInfo alloc] init];
        userInfo.userId = userId;
        userInfo.name = @"333333";
        userInfo.portraitUri = @"http://www.mf08s.com/y/q/UploadFiles_q/20121005/2012100507413841.jpg";
        return completion(userInfo);
    }
    else
    {
        RCUserInfo *userInfo = [[RCUserInfo alloc] init];
        userInfo.userId = userId;
        userInfo.name = @"222222";
        userInfo.portraitUri = @"https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=479216235,3399005187&fm=27&gp=0.jpg";
        return completion(userInfo);
    }
    return completion(nil);
    
    // return completion(user);
}

// 自定义cell


//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//{
//    return 1;
//}
//
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//{
//
//    return  12;
//}
//
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//
//    //    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCell"];
//    //    if (!cell) {
//    //        cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"TableViewCell"];
//    //
//    //    }
//    static NSString *CellIdentifier = @"TableViewCell";
//    // 通过indexPath创建cell实例 每一个cell都是单独的
//    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//    // 判断为空进行初始化  --（当拉动页面显示超过主页面内容的时候就会重用之前的cell，而不会再次初始化）
//
//    if (!cell) {
//        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
//        cell.selectionStyle = UITableViewCellSelectionStyleNone;
//        cell.width = tableView.width;
//        cell.height = kCellHeight;
//    }
//    NSInteger space = 10;
//
//        UIImageView *userImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kCellHeight*0.7,kCellHeight*0.7 )];
//        userImage.center = CGPointMake(space+userImage.width/2, cell.height/2);
////        userImage.backgroundColor = [UIColor redColor];
//        userImage.layer.masksToBounds = YES;
//        userImage.layer.cornerRadius = 8;
//        [cell addSubview:userImage];
//    [userImage sd_setImageWithURL:[NSURL URLWithString:@"http://www.qqpk.cn/Article/UploadFiles/201304/20130425095933703.jpg"]];
//    userImage.userInteractionEnabled = YES;
//    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toOthersProfile)];
//    //为图片添加手势
//    [userImage addGestureRecognizer:singleTap];
//
//        UILabel *nameLab = [[UILabel alloc] initWithFrame:CGRectMake(userImage.x+userImage.width+space, userImage.y, cell.width - userImage.width - 3*space, userImage.height/3.5)];
////        nameLab.backgroundColor = [UIColor yellowColor];
//        [cell addSubview:nameLab];
//    nameLab.font=[UIFont fontWithName:kFontName size:nameLab.height*0.6];
//    nameLab.text=@"Concierge";
//
//        UILabel *infoLab = [[UILabel alloc] initWithFrame:CGRectMake(nameLab.x, nameLab.y+nameLab.height, nameLab.width, userImage.height/4)];
////        infoLab.backgroundColor = [UIColor greenColor];
//        [cell addSubview:infoLab];
//    infoLab.font=[UIFont fontWithName:kFontName size:infoLab.height*0.5];
//    infoLab.text = @"35.Marina/West Village.5 ' l ";
//
//        UILabel *educaLab = [[UILabel alloc] initWithFrame:CGRectMake(infoLab.x, infoLab.y+infoLab.height, nameLab.width, userImage.height/4)];
////        educaLab.backgroundColor = [UIColor redColor];
//        [cell addSubview:educaLab];
//    educaLab.font=[UIFont fontWithName:kFontName size:educaLab.height*0.5];
//    educaLab.textColor = kEducaLabFontColor;
//    educaLab.text = @"Goldman Sachs. Harvard Law School";
//
//    UILabel *timeLab = [[UILabel alloc] initWithFrame:nameLab.frame];
//    timeLab.font=[UIFont fontWithName:kFontName size:timeLab.height*0.4];
//    timeLab.textAlignment = NSTextAlignmentRight;
//    timeLab.text = @"21 day ago";
//   timeLab.textColor = kTimeFontColor;
//    [cell addSubview:timeLab];
//
//    if (selectIndex == 0) {
//
//    }
//
//
//     if (selectIndex == 1)
//    {
//        UIButton *rematch = [[UIButton alloc] initWithFrame:CGRectMake(educaLab.x, educaLab.y+educaLab.height, nameLab.width, userImage.height/3.5)];
//        rematch.backgroundColor = kAppColor;
//        rematch.layer.masksToBounds = YES;
//        rematch.layer.cornerRadius = 8;
//        [cell addSubview:rematch];
//        [rematch setTitle:[bundle localizedStringForKey:@"rematch" value:nil table:@"hello"] forState:UIControlStateNormal];
//        rematch.titleLabel.font=[UIFont fontWithName:kFontName size:rematch.height*0.5];
//    }
//    else
//    {
//        UILabel *messagesLab = [[UILabel alloc] initWithFrame:CGRectMake(educaLab.x, educaLab.y+educaLab.height, nameLab.width,userImage.height/4)];
////        messagesLab.backgroundColor = [UIColor brownColor];
//        [cell addSubview:messagesLab];
//        messagesLab.font=[UIFont fontWithName:kFontName size:messagesLab.height*0.5];
//        messagesLab.text = @"Don't be a flake- send a message!";
//
//        if (indexPath.row == 0 ) {
//            messagesLab.textColor = kAppColor;
//        }
//    }
//    return cell;
//}
//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    NSLog(@"row:%@ - section:%@",@(indexPath.row) , @(indexPath.section));
//    RYMessageViewController *vc = [[RYMessageViewController alloc] init];
//    vc.conversationType = ConversationType_PRIVATE|ConversationType_DISCUSSION|ConversationType_GROUP;
//    vc.targetId = @"xxx";
//    vc.title = @"test";
//    [self.navigationController pushViewController:vc animated:YES];
////    RYMessageViewController *chat = [[RYMessageViewController alloc] initWithConversationType:conversationType
////targetId:targetId];
////
////    //设置会话的类型，如单聊、讨论组、群聊、聊天室、客服、公众服务会话等
////    chat.conversationType = ConversationType_PRIVATE;
////    //设置会话的目标会话ID。（单聊、客服、公众服务会话为对方的ID，讨论组、群聊、聊天室为会话的ID）
////    chat.targetId = @"targetIdYouWillChatIn";
////
////    //设置聊天会话界面要显示的标题
////    chat.title = @"想显示的会话标题";
////    //显示聊天会话界面
////    [self.navigationController pushViewController:chat animated:YES];
//}
////- (void)onSelectedTableRow:(RCConversationModelType)conversationModelType
////         conversationModel:(RCConversationModel *)model
////               atIndexPath:(NSIndexPath *)indexPath {
////    RCConversationViewController *conversationVC = [[RCConversationViewController alloc]init];
////    conversationVC.conversationType = model.conversationType;
////    conversationVC.targetId = model.targetId;
////    conversationVC.title = @"想显示的会话标题";
////    [self.navigationController pushViewController:conversationVC animated:YES];
////}
//-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return kCellHeight;
//}
////-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
////{
////
////}
//- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
//{
//
//}
//
///**
// *  左滑cell时出现什么按钮
// */
//- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    UITableViewRowAction *action0 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"关注" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
//
//    }];
//
//    UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
//
//    }];
//
//    UITableViewRowAction *action2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"其他" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
//
//    }];
//    return @[action0, action1, action2];
//}
//-(void)toOthersProfile
//{
//    OthersProfileViewController *otherProVc = [[OthersProfileViewController alloc] init];
//    [self.navigationController pushViewController:otherProVc animated:YES];
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
